<?php

include_once('user_controller.php');

class Canvassing_Sheets extends User_Controller
{

	public function __construct()
	{
		parent::__construct();
        $this->load->model('electoral_roll_model');
        $this->load->model('person_model');
		$this->load->library('session');
	}

	public function view($zone)
	{
		$this->load->helper('url');

	    $this->load->library('pagination');
	    $config['base_url'] = base_url().'index.php/canvassing_sheets/view/' . $zone;
	    $config['uri_segment'] = 4;
	    $config['per_page'] = 50;
	    $config['full_tag_open'] = '<p>';
	    $config['full_tag_close'] = '</p>';

		$data['roll'] = $zone;

	    $data['title'] = 'Canvassing Sheet ' . $zone; //?? sort this

		$street_search = $this->session->userdata('street_search');

		if (!$street_search)
		{
			$street_search = "";
		}

		$data['street_search'] = $street_search;

//		echo "name_search: [" . $name_search . "]. address_search: [" . $address_search . "]<br/>";

		if (!$street_search)
		{
			// no entries to display
	    	$config['total_rows'] = 0;
			$data['entries'] = array();
		}
		else
		{
	    	$config['total_rows'] = $this->electoral_roll_model->count_canvass_sheet($zone, $street_search);
            $data['entries'] = $this->get_canvass_sheet($zone, $street_search, $config['per_page'], (int) $this->uri->segment(4));
//            $data['entries'] = $this->electoral_roll_model->get_canvass_sheet($zone, $street_search, $config['per_page'], (int) $this->uri->segment(4));
		}

        $zone_type = $this->session->userdata('zone_type');

		$data['rolls'] = $this->electoral_roll_model->get_electoral_zones($zone_type);

		$this->pagination->initialize($config);

		$data['content'] = 'canvassing_sheet_display';
		$this->load->view('layout/main', $data);

	}

    public function create_printable($roll)
    {
        // ?? very similar code to view. should try and extract a common method
        $this->load->helper('url');

        $data['roll'] = $roll;

        $data['title'] = 'Canvassing Sheet ' . $roll; //?? sort this

        $street_search = $this->session->userdata('street_search');

        if (!$street_search)
        {
            $street_search = "";
        }

        $data['street_search'] = $street_search;

//      echo "name_search: [" . $name_search . "]. address_search: [" . $address_search . "]<br/>";

        if (!$street_search)
        {
            // no entries to display
            $config['total_rows'] = 0;
            $data['entries'] = array();
        }
        else
        {
            $config['total_rows'] = $this->electoral_roll_model->count_canvass_sheet($roll, $street_search);
            $data['entries'] = $this->get_canvass_sheet($roll, $street_search, -1, 0);
//            $data['entries'] = $this->electoral_roll_model->get_canvass_sheet($roll, $street_search, -1, 0);
        }

        $data['content'] = 'canvassing_sheet_print';
        $this->load->view('layout/printable', $data);

    }

    public function get_canvass_sheet($roll, $street, $num, $offset)
    {
        $entries = $this->electoral_roll_model->get_canvass_sheet($roll, $street, $num, $offset);
        
        foreach ($entries as $row)
        {
            $voting_record = $this->person_model->get_voting_record ($row->id);
            
            if (!empty ($voting_record))
            {
                $row->last_voted = $voting_record[0]->election_date;
                $row->election_type = $voting_record[0]->election_type;
                $row->vote_type = $voting_record[0]->voted;
            }
            else
            {
                $row->last_voted = NULL;
                $row->election_type = NULL;
                $row->vote_type = NULL;
            }
                        
            $contacts = $this->person_model->get_contacts ($row->id);
            if (!empty ($contacts))
            {
                $row->last_contacted = $contacts[0]->contact_date;
                $row->contacted_by = $contacts[0]->contacted_by;
                $row->party = $contacts[0]->party;
                $row->notes = $contacts[0]->notes;
                $row->rank = $contacts[0]->rank;
            }            
            else
            {
                $row->last_contacted = NULL;
                $row->contacted_by = NULL;
                $row->party = NULL;
                $row->notes = NULL;
                $row->rank = NULL;
            }
        } 
        
        return $entries;
    }
    
    public function create_pdf($roll)
    {
        $this->load->library('cezpdf');
 
        // ?? very similar code to view. should try and extract a common method
        
        $data['roll'] = $roll;

        $data['title'] = 'Canvassing Sheet ' . $roll; //?? sort this

        $street_search = $this->session->userdata('street_search');

        if (!$street_search)
        {
            $street_search = "";
        }

        $data['street_search'] = $street_search;

//      echo "name_search: [" . $name_search . "]. address_search: [" . $address_search . "]<br/>";

        if (!$street_search)
        {
            // no entries to display
            $config['total_rows'] = 0;
            $data['entries'] = array();
        }
        else
        {
            $config['total_rows'] = $this->electoral_roll_model->count_canvass_sheet($roll, $street_search);
            $data['entries'] = $this->electoral_roll_model->get_canvass_sheet($roll, $street_search, -1, -1);
        }

        $title = $data['title'] . "(" . $data['street_search'] . ")";
 
        $col_names = array(
            'name' => 'Name',
            'phone' => 'Phone Number',
            'email' => 'E-mail Address'
        );
        
        $this->cezpdf->ezTable($data['entries'], $col_names, $title, array('width'=>550));
        
//        $this->cezpdf->ezText($title, 12, array('justification' => 'center'));
//        $this->cezpdf->ezSetDy(-10);
 
//        $content = 'The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog. Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.';
 
//        $this->cezpdf->ezText($content, 10);
 
        $this->cezpdf->ezStream();
        
//        redirect('canvassing_sheets/view/' . $roll);
    }

	public function submit_search()
	{
		$this->load->helper('url');

		$this->session->unset_userdata('street_search');

		$roll = $this->input->post('roll');

        $street_search = trim ($this->input->post('street'));

		$terms = array();

		if (strlen ($street_search) > 0)
		{
			$terms ['street_search'] = $street_search;
		}

		if (count ($terms) > 0)
		{
            $this->session->set_userdata($terms);
        }

        redirect('canvassing_sheets/view/' . $roll);
	}
}
?>